package com.lgmshare.component.network;

import android.content.Context;

import com.lgmshare.component.network.okhttp.OkHttpRequestClient;

/**
 * HTTP通信类,支持GET、POST方式。使用时需要先初始化，建议在application中
 * 可以使用{@link #cancel(Object)},{@link #cancelAll()}取消请求
 *
 * @author lim
 * @description: TODO
 * @email lgmshare@gmail.com
 * @datetime 2016/5/6 10:04
 */
public final class HttpRequestClient {

    private static final String TAG = "HttpRequestClient";

    public static final int HTTP_OK = 200;
    /**
     * 未知错误code
     */
    public static final int HTTP_UNEXPECTED_CODE = -100;
    /**
     * 默认HTTP连接超时时间
     */
    public static final int CONNECT_TIMEOUT = 60;

    public static final String HEADER_CONTENT_TYPE = "Content-Type";
    public static final String HEADER_CONTENT_RANGE = "Content-Range";
    public static final String HEADER_CONTENT_ENCODING = "Content-Encoding";
    public static final String HEADER_CONTENT_DISPOSITION = "Content-Disposition";
    public static final String HEADER_ACCEPT_ENCODING = "Accept-Encoding";
    public static final String CONTENT_TYPE_JSON = "application/json; charset=utf-8";
    public static final String CONTENT_TYPE_PLAIN = "text/plain; charset=utf-8";
    public static final String CONTENT_TYPE_FILE = "application/octet-stream; charset=utf-8";
    public static final String CONTENT_TYPE_IMG = "image/png; charset=utf-8";
    public static final String CONTENT_TYPE_FORM = "application/x-www-form-urlencoded; charset=utf-8";
    public static final String ENCODING_GZIP = "gzip";

    private static RequestClient sRequestClient;

    private HttpRequestClient() {
    }

    /**
     * 初始化
     *
     * @param context
     */
    public static void init(Context context) {
        init(context, null);
    }

    public static void init(Context context, HttpConfiguration configuration) {
        if (sRequestClient != null) {
            return;
        }
        if (configuration == null) {
            configuration = new HttpConfiguration.Builder().build();
        }
        sRequestClient = new OkHttpRequestClient(context, configuration);
    }

    public static void get(Object tag, String url, HttpResponseCallback callback) {
        get(tag, url, null, callback);
    }

    public static void get(Object tag, String url, RequestParams params, HttpResponseCallback callback) {
        get(tag, url, params, null, callback);
    }

    public static void get(Object tag, String url, RequestParams params, RequestHeaders headers, HttpResponseCallback callback) {
        sRequestClient.get(tag, url, params, headers, callback);
    }

    public static void post(Object tag, String url, HttpResponseCallback callback) {
        sRequestClient.post(tag, url, callback);
    }

    public static void post(Object tag, String url, String data, HttpResponseCallback callback) {
        sRequestClient.post(tag, url, data, callback);
    }

    public static void post(Object tag, String url, String data, String contentType, HttpResponseCallback callback) {
        sRequestClient.post(tag, url, data, contentType, callback);
    }

    public static void post(Object tag, String url, String data, String contentType, RequestHeaders headers, HttpResponseCallback callback) {
        sRequestClient.post(tag, url, data, contentType, headers, callback);
    }

    public static void put(Object tag, String url, String data, String contentType, RequestHeaders headers, HttpResponseCallback callback) {
        sRequestClient.put(tag, url, data, contentType, headers, callback);
    }

    public static void delete(Object tag, String url, String data, String contentType, RequestHeaders headers, HttpResponseCallback callback) {
        sRequestClient.delete(tag, url, data, contentType, headers, callback);
    }

    public static void post(Object tag, String url, RequestParams params, HttpResponseCallback callback) {
        sRequestClient.post(tag, url, params, callback);
    }

    public static void post(Object tag, String url, RequestParams params, RequestHeaders headers, HttpResponseCallback callback) {
        sRequestClient.post(tag, url, params, headers, callback);
    }

    public static void put(Object tag, String url, RequestParams params, RequestHeaders headers, HttpResponseCallback callback) {
        sRequestClient.put(tag, url, params, headers, callback);
    }

    public static void delete(Object tag, String url, RequestParams params, RequestHeaders headers, HttpResponseCallback callback) {
        sRequestClient.delete(tag, url, params, headers, callback);
    }

    public static void syncGet(Object tag, String url, RequestParams params, RequestHeaders headers, HttpResponseCallback callback) {
        sRequestClient.syncGet(tag, url, params, headers, callback);
    }

    public static void syncPost(Object tag, String url, RequestParams params, RequestHeaders headers, HttpResponseCallback callback) {
        sRequestClient.syncPost(tag, url, params, headers, callback);
    }

    /**
     * 取消连接请求
     *
     * @param tag
     */
    public static void cancel(Object tag) {
        sRequestClient.cancel(tag);
    }

    /**
     * 取消连接请求 ALL
     */
    public static void cancelAll() {
        sRequestClient.cancelAll();
    }
}
